package com.sihai.ehr.web.action.train;

import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.sihai.core.LimitedList;
import com.sihai.core.beans.BeanInject;
import com.sihai.ehr.model.biz.EmployeeinfoWork;
import com.sihai.ehr.model.biz.TrainAssess;
import com.sihai.ehr.model.biz.TrainAssessWeight;
import com.sihai.ehr.model.biz.TrainCategory;
import com.sihai.ehr.model.biz.TrainPlan;
import com.sihai.ehr.model.sys.Organization;
import com.sihai.ehr.service.EmployeeService;
import com.sihai.ehr.service.OrganizationService;
import com.sihai.ehr.service.TrainAssessService;
import com.sihai.ehr.service.TrainAssessWeightService;
import com.sihai.ehr.service.TrainCategoryService;
import com.sihai.ehr.service.TrainPlanEmployeeTargetService;
import com.sihai.ehr.service.TrainPlanService;
import com.sihai.ehr.web.action.EHRAction;
import com.sihai.ehr.web.pojo.TrainAssessPojo;

/**
 * 培训类型控制器
 * 
 * @author henry
 * 
 */
@Results({
		@Result(name = "main", location = "/WEB-INF/pages/train/train_main.jsp"),
		@Result(name = "list", location = "assess_list.jsp"),
		@Result(name = "assess_outing_list", location = "assess_outing_list.jsp"),
		@Result(name = "assess_outing_close_list", location = "assess_outing_close_list.jsp"),
		@Result(name = "assess_person_list", location = "/WEB-INF/pages/train/assess_person_list.jsp"),
		@Result(name = "assess_outing_person_list", location = "/WEB-INF/pages/train/assess_outing_person_list.jsp"),
		@Result(name = "train_assess_employee", location = "assess_employee.jsp"),
		@Result(name = "train_assess_outing_employee", location = "assess_outing_employee.jsp"),
		@Result(name = "assess_outing_list_tab", location = "assess_outing_list_tab.jsp")})
public class TrainAssessAction extends EHRAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Log log = LogFactory.getLog(TrainAssessAction.class);

	@BeanInject
	private OrganizationService organizationService;
	@BeanInject
	private TrainPlanService trainPlanService;
	@BeanInject
	private TrainAssessService trainAssessService;
	@BeanInject
	private TrainAssessWeightService trainAssessWeightService;
	@BeanInject
	private TrainPlanEmployeeTargetService trainPlanEmployeeTargetService;
	@BeanInject
	private EmployeeService employeeService;

	private List<Organization> organizations;
	private LimitedList<TrainPlan> plans;
	private TrainPlan plan;
	private String weightTitle;
	private Long planId;
	private LimitedList<TrainAssessPojo> assessPojos;
	// -----------评论块---------
	private Long assessId;
	private int assessType;
	private Long weightId;
	private Long workId;

	@Action("/train/plan_assess_main")
	@Override
	public String execute() throws Exception {
		listTitle = "教育训练评价";
		listAction = "/train/assess_list.xhtml";
		organizations = organizationService
				.findChildren(null, true, null, null);
		if (organizations != null && organizations.size() > 0) {
			if(getCurrentUserId() > 0){
            	Long topOrgId = getCurrentUser().getWork().getOrganization().getTopOrganization().getId();
            	Iterator<Organization> it = organizations.iterator();
            	while(it.hasNext()){
            		Organization o = it.next();
            		if(o.getId() != topOrgId){
            			it.remove();
            		}
            	}
            }
			organizationId = organizations.get(0).getId();
		}
		showChildOrg = true;
		return "main";
	}

	@Action("/train/assess_list")
	public String list() {
		listAction = "/train/assess_list.xhtml";
		plans = trainPlanService.getTrainPlans(organizationId, -1L, 0,null,
				createLimit(), createSorter());
		paging(plans);
		/* 设置翻页action */
		this.setPageUrl("train/assess_list.xhtml");
		return "list";
	}

	@Action("/train/train_assess_employee")
	public String getAssessListByPlanId() {
		try {
			assessPojos = trainAssessService.getTrainAssessPojos(planId,
					weightTitle, 0, null, null);
			TrainAssessWeight assessWeight = trainAssessWeightService
					.getTrainAssessWeightByTitle(weightTitle, 0);
			if(assessWeight != null){
				weightId = assessWeight.getId();
			}
			if(assessPojos != null && assessPojos.size() > 0){
				for (TrainAssessPojo pojo : assessPojos) {
					if(pojo.getWeightId() <= 0){
						pojo.setWeightId(weightId);
					}
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return "train_assess_employee";
	}

	/**
	 * 提交培训评价
	 * 
	 * @return
	 */
	@Action(value = "/train/post_assess", results = { @Result(name = "success", type = "plainText") })
	public String postTrainAssess() {
		if (planId < 0 || weightId < 0 || workId < 0) {
			return ERROR;
		}
		TrainAssessWeight assessWeight = trainAssessWeightService
				.getTrainAssessWeightByTitle(weightTitle, assessType);
		TrainPlan plan = trainPlanService.getTrainPlan(planId);
		EmployeeinfoWork work = employeeService.getEmployeeinfoWork(workId);
		TrainAssess mAssess;
		if (assessId > 0) {
			// 修改
			mAssess = trainAssessService.getTrainAssess(assessId);
		} else {
			mAssess = trainAssessService.getTrainAssessByPlanWeight(planId, weightTitle, assessType, workId);
		}
		if(mAssess == null){
			mAssess = new TrainAssess();
			mAssess.setTrainAssessWeight(assessWeight);
			mAssess.setTrainPlan(plan);
			mAssess.setEmployeeinfoWork(work);
			mAssess.setAssessType(plan.getLecturerType());
		}
		try {
			mAssess.setScore(Float.parseFloat(getParameter("txt_score")));
			mAssess.setAssessContent(getParameter("txt_content"));
			trainAssessService.save(mAssess);
			
			//外训结案数更新
			if(assessType  > 0){	
				//统计评论员工数
				Long requestAssessEmployeeNum = trainPlanEmployeeTargetService.getTrainPlanEmployeeTargetNum(planId);
				//统计外训框架中已评论人数
				Long hasAssessEmployeeNum = trainAssessService.getTrainAssessNumByWeight(planId, weightTitle, assessType,workId);
				if((hasAssessEmployeeNum + 1) >= requestAssessEmployeeNum){
					plan.setCloseAuditNum(plan.getCloseAuditNum() + 1);
					trainPlanService.save(plan);
				}
			}
			return SUCCESS;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return ERROR;
	}

	// -------------外训评价模块-----------------
	@Action("/train/plan_assess_outing_main")
	public String dispatchOutAssessMain() throws Exception {
		listTitle = "外训评价";
		listAction = "/train/assess_outing_list.xhtml";
		organizations = organizationService
				.findChildren(null, true, null, null);
		if (organizations != null && organizations.size() > 0) {
			if(getCurrentUserId() > 0){
            	Long topOrgId = getCurrentUser().getWork().getOrganization().getTopOrganization().getId();
            	Iterator<Organization> it = organizations.iterator();
            	while(it.hasNext()){
            		Organization o = it.next();
            		if(o.getId() != topOrgId){
            			it.remove();
            		}
            	}
            }
			organizationId = organizations.get(0).getId();
		}
		showChildOrg = true;
		return "main";
	}

	@Action("/train/assess_outing_list")
	public String dispatchOutAssessList() {
		listAction = "/train/assess_outing_list.xhtml";
		plans = trainPlanService.getTrainPlans(organizationId, -1L, 1,null,
				createLimit(), createSorter());
		paging(plans);
		/* 设置翻页action */
		this.setPageUrl("train/assess_outing_list.xhtml");
		return "assess_outing_list";
	}
	@Action("/train/assess_outing_list_tab")
	public String dispatchOutAssessTabList() {
		 plan = trainPlanService.getTrainPlan(planId);
		 
		return "assess_outing_list_tab";
	}
	
	
	// -------------外训结案模块-----------------
//	@Action("/train/plan_assess_outing_close_main")
//	public String dispatchOutAssessCloseMain() throws Exception {
//		listTitle = "外训评价";
//		listAction = "/train/assess_outing_close_list.xhtml";
//		organizations = organizationService
//				.findChildren(null, true, null, null);
//		if (organizations != null && organizations.size() > 0) {
//			organizationId = organizations.get(0).getId();
//		}
//		showChildOrg = true;
//		return "main";
//	}
//
//	@Action("/train/assess_outing_close_list")
//	public String dispatchOutAssessCloseList() {
//		listAction = "/train/assess_outing_list.xhtml";
//		plans = trainPlanService.getTrainPlans(organizationId, -1L, 1,
//				createLimit(), createSorter());
//		paging(plans);
//		return "assess_outing_close_list";
//	}
		
	// 外训评价人员
	@Action("/train/train_assess_outing_employee")
	public String getAssessOutingListByPlanId() {
		try {
			assessPojos = trainAssessService.getTrainAssessPojos(planId,
					weightTitle, 1, null, null);
			TrainAssessWeight assessWeight = trainAssessWeightService
					.getTrainAssessWeightByTitle(weightTitle, 1);
			if(assessWeight != null){
				weightId = assessWeight.getId();
			}
			if(assessPojos != null && assessPojos.size() > 0){
				for (TrainAssessPojo pojo : assessPojos) {
					if(pojo.getWeightId() <= 0){
						pojo.setWeightId(weightId);
					}
				}
			}

		} catch (Exception e) {
			// TODO: handle exception
		}
		return "train_assess_outing_employee";
	}

	@Action("/public/plan_assess_person_main")
	public String executePerson() throws Exception {
		listTitle = "教育训练评价";
		listAction = "/public/assess_person_list.xhtml";
		organizations = organizationService
				.findChildren(null, true, null, null);
		if (organizations != null && organizations.size() > 0) {
			if(getCurrentUserId() > 0){
            	Long topOrgId = getCurrentUser().getWork().getOrganization().getTopOrganization().getId();
            	Iterator<Organization> it = organizations.iterator();
            	while(it.hasNext()){
            		Organization o = it.next();
            		if(o.getId() != topOrgId){
            			it.remove();
            		}
            	}
            }
			organizationId = organizations.get(0).getId();
		}
		showChildOrg = true;
		return "main";
	}

	@Action("/public/assess_person_list")
	public String personList() {
		listAction = "/public/assess_person_list.xhtml";
		plans = trainPlanService.getTrainPlansByUserId(organizationId, -1L,
				getCurrentUserId(), createLimit(), createSorter());
		paging(plans);
		/* 设置翻页action */
		this.setPageUrl("public/assess_person_list.xhtml");
		return "assess_person_list";
	}

	@Action("/public/plan_assess_outing_main")
	public String dispatchOutPersonAssessMain() throws Exception {
		listTitle = "外训评价";
		listAction = "/public/assess_outing_person_list.xhtml";
		organizations = organizationService
				.findChildren(null, true, null, null);
		if (organizations != null && organizations.size() > 0) {
			if(getCurrentUserId() > 0){
            	Long topOrgId = getCurrentUser().getWork().getOrganization().getTopOrganization().getId();
            	Iterator<Organization> it = organizations.iterator();
            	while(it.hasNext()){
            		Organization o = it.next();
            		if(o.getId() != topOrgId){
            			it.remove();
            		}
            	}
            }
			organizationId = organizations.get(0).getId();
		}
		showChildOrg = true;
		return "main";
	}

	@Action("/public/assess_outing_person_list")
	public String dispatchOutPersonAssessList() {
		listAction = "/public/assess_outing_person_list.xhtml";
		plans = trainPlanService.getTrainPlansByUserId(organizationId, -1L,
				getCurrentUserId(), createLimit(), createSorter());
		paging(plans);
		/* 设置翻页action */
		this.setPageUrl("public/assess_outing_person_list.xhtml");
		return "assess_outing_person_list";
	}

	public void setOrganizations(List<Organization> organizations) {
		this.organizations = organizations;
	}

	public List<Organization> getOrganizations() {
		return organizations;
	}

	public LimitedList<TrainPlan> getPlans() {
		return plans;
	}

	public void setPlans(LimitedList<TrainPlan> plans) {
		this.plans = plans;
	}
	
	public TrainPlan getPlan() {
		return plan;
	}
	
	public void setPlan(TrainPlan plan) {
		this.plan = plan;
	}

	public String getWeightTitle() {
		return weightTitle;
	}

	public void setWeightTitle(String weightTitle) {
		this.weightTitle = weightTitle;
	}

	public Long getPlanId() {
		return planId;
	}

	public void setPlanId(Long planId) {
		this.planId = planId;
	}

	public LimitedList<TrainAssessPojo> getAssessPojos() {
		return assessPojos;
	}

	public void setAssessPojos(LimitedList<TrainAssessPojo> assessPojos) {
		this.assessPojos = assessPojos;
	}

	public Long getAssessId() {
		return assessId;
	}

	public void setAssessId(Long assessId) {
		this.assessId = assessId;
	}
	
	public int getAssessType() {
		return assessType;
	}
	
	public void setAssessType(int assessType) {
		this.assessType = assessType;
	}

	public Long getWeightId() {
		return weightId;
	}

	public void setWeightId(Long weightId) {
		this.weightId = weightId;
	}

	public Long getWorkId() {
		return workId;
	}

	public void setWorkId(Long workId) {
		this.workId = workId;
	}

}
